package com.teatrace.action.produce;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.teatrace.model.base.Device;
import com.teatrace.model.base.Employee;
import com.teatrace.model.produce.AirCheck;
import com.teatrace.model.produce.AirCheckDetail;
import com.teatrace.model.produce.SoilCheck;
import com.teatrace.model.produce.SoilCheckDetail;
import com.teatrace.model.produce.TeaTrees;
import com.teatrace.model.produce.TeaTreesBreed;
import com.teatrace.model.produce.TeaTreesCheck;
import com.teatrace.model.produce.TeaTreesCheckDetail;
import com.teatrace.model.produce.TeaTreesPrevention;
import com.teatrace.service.produce.ProduceService;
import com.teatrace.service.produce.impl.ProduceServiceImpl;

public class GrowthAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final Logger log = Logger.getLogger(GrowthAction.class);
	
	public ProduceService produceService;
	
	HttpServletRequest request ;
	
	private int teaTreesId;
	
	private AirCheckDetail airCheckDetail;
	private AirCheck airCheck;
	private int airCheckId;
	
	private SoilCheckDetail soilCheckDetail;
	private SoilCheck soilCheck;
	private int soilCheckId;
	
	private TeaTreesCheckDetail teaTreesCheckDetail;
	private TeaTreesCheck teaTreesCheck;
	private int teaTreesCheckId;
	
	private String emps;
	private String devices;

	private int teaTreesBreedId;
	private  TeaTreesBreed teaTreesBreed;
	
	private int teaTreesPreventionId;
	private TeaTreesPrevention teaTreesPrevention;
	
	public GrowthAction(){
		produceService = new ProduceServiceImpl();
		request = ServletActionContext.getRequest();
	}
	
	/**
	 * 列出茶林
	 */
	public String listTeaTrees(){
		log.debug("列出茶林信息");
		List<TeaTrees> list;
		try {
			list = produceService.listByPage(TeaTrees.class, 0);//0是页数
			request.setAttribute("listTeaTrees", list);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
			return ERROR;
		}	
	}
	
	//************************检测操作*****************************//
	/**
	 * 列出某个茶林的检测检测信息
	 */
	public String listCheckInfo(){
	
		log.debug("列出茶林   "+teaTreesId+" 的检测信息");
		try {
			List<AirCheck> listAirCheck = produceService.listByCriteria(AirCheck.class, 0,"teaTrees.id",teaTreesId);
			List<SoilCheck> listSoilCheck = produceService.listByCriteria(SoilCheck.class, 0,"teaTrees.id",teaTreesId);
			List<TeaTreesCheck> listTeaTreesCheck = produceService.listByCriteria(TeaTreesCheck.class, 0,"teaTrees.id",teaTreesId);
			
			request.setAttribute("listAirCheck", listAirCheck);
			request.setAttribute("listSoilCheck", listSoilCheck);
			request.setAttribute("listTeaTreesCheck", listTeaTreesCheck);
			
			request.setAttribute("teatreesId", teaTreesId);
			
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.debug(e.getMessage());
			request.setAttribute("msg", "列出检测信息");
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}

	
	//************************空气检测操作****************************//
	/**
	 * 增加空气检测信息
	 */
	public String addAirCheck(){
		log.debug("增加空气检测信息");
		request.setAttribute("msg", "增加空气检测信息");
		try {
			TeaTrees teaTrees = (TeaTrees) produceService.loadById(TeaTrees.class, teaTreesId);
			
			airCheckDetail.setDescription(airCheck.getDescription());
			
			airCheck.setDetail(airCheckDetail);
			
		    Set<Employee> airCheckEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
		    Set<Device> airCheckDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
		    
		    airCheck.setEmps(airCheckEmpsSet);
		    airCheck.setDevices(airCheckDevicesSet);
		    airCheck.setDeleted(false);
		    airCheck.setTeaTrees(teaTrees);
		    
		    produceService.add(airCheck);
		    
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 删除一个空气检测信息
	 * @return
	 */
	public String deleteAirCheck(){
		log.debug("删除空气检测信息,空气检测的id是"+airCheckId);
		try {
			produceService.deleteById(AirCheck.class,airCheckId);
			request.setAttribute("msg", "删除空气检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause",e.getMessage());
			return ERROR;
		}
		
	}
	
	public String getAirCheckById(){
		log.debug("空气检测的id是"+airCheckId+",正在跳转到更新操作页面...");
		try {
			AirCheck airCheck = (AirCheck) produceService.getById(AirCheck.class, airCheckId);
	
			request.setAttribute("airCheck", airCheck);
			
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause","更新空气检测 "+airCheckId+"，跳转到更新操作页面");
			return ERROR;
		}
	}
	/**
	 * 更新空气检测信息
	 * @return
	 */
	public String updateAirCheck(){
		log.debug("更新空气检测信息");
		try {
			Set<Employee> airCheckEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> airCheckDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			airCheck.setEmps(airCheckEmpsSet);
			airCheck.setDevices(airCheckDevicesSet);
			
			airCheck.getDetail().setDescription(airCheck.getDescription());
			produceService.update(airCheck);
			request.setAttribute("msg", "更新空气检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
		}
		return ERROR;
	}
	
	

	//************************土壤检测操作****************************//
	/**
	 * 增加土壤检测信息
	 */
	public String addSoilCheck(){
		log.debug("增加土壤检测信息");
		
		try {
			TeaTrees teaTrees = (TeaTrees) produceService.loadById(TeaTrees.class, teaTreesId);
			
			soilCheckDetail.setDescription(soilCheck.getDescription());
			
			soilCheck.setDetail(soilCheckDetail);
			
		    Set<Employee> empSet = ActionUtils.getInstance().getEmployees(emps.trim());
		    Set<Device> deviceSet = ActionUtils.getInstance().getDevices(devices.trim());
		    
		    soilCheck.setEmps(empSet);
		    soilCheck.setDevices(deviceSet);
		    soilCheck.setDeleted(false);
		    soilCheck.setTeaTrees(teaTrees);
		    
		    produceService.add(soilCheck);
		    
		    request.setAttribute("msg", "增加土壤检测信息");
		    
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 删除一个土壤检测信息
	 * @return
	 */
	public String deleteSoilCheck(){
		log.debug("删除土壤检测信息,土壤检测的id是"+soilCheckId);
		try {
			produceService.deleteById(SoilCheck.class,soilCheckId);
			request.setAttribute("msg", "删除土壤检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause",e.getMessage());
			return ERROR;
		}
		
	}
	
	public String getSoilCheckById(){
		log.debug("土壤检测的id是"+soilCheckId+",正在跳转到更新操作页面...");
		try {
			SoilCheck soilCheck = (SoilCheck) produceService.getById(AirCheck.class, soilCheckId);
	
			request.setAttribute("soilCheck", soilCheck);
			
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause","更新土壤检测 "+soilCheckId+"，跳转到更新操作页面");
			return ERROR;
		}
	}
	/**
	 * 更新土壤检测信息
	 * @return
	 */
	public String updateSoilCheck(){
		log.debug("更新土壤检测信息");
		try {
			Set<Employee> empSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> deviceSet = ActionUtils.getInstance().getDevices(devices.trim());
			soilCheck.setEmps(empSet);
			soilCheck.setDevices(deviceSet);
			soilCheck.getDetail().setDescription(soilCheck.getDescription());
			produceService.update(soilCheck);
			request.setAttribute("msg", "更新土壤检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
		}
		return ERROR;
	}
	
	
	
	
	
	

	//************************茶林检测操作****************************//
	/**
	 * 增加茶林检测信息
	 */
	public String addTeaTreesCheck(){
		log.debug("增加茶林检测信息");
		
		try {
			TeaTrees teaTrees = (TeaTrees) produceService.loadById(TeaTrees.class, teaTreesId);
			teaTreesCheckDetail.setDescription(teaTreesCheck.getDescription());
			teaTreesCheck.setDetail(teaTreesCheckDetail);
		    Set<Employee> empsSet = ActionUtils.getInstance().getEmployees(emps.trim());
		    Set<Device> devicesSet = ActionUtils.getInstance().getDevices(devices.trim());
		    teaTreesCheck.setEmps(empsSet);
		    teaTreesCheck.setDevices(devicesSet);
		    teaTreesCheck.setTeaTrees(teaTrees);
		    produceService.add(teaTreesCheck);
		    request.setAttribute("msg", "增加茶林检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 删除一个茶林检测信息
	 * @return
	 */
	public String deleteTeaTreesCheck(){
		log.debug("删除茶林检测信息,茶林检测的id是"+teaTreesCheckId);
		try {
			produceService.deleteById(TeaTreesCheck.class,teaTreesCheckId);
			request.setAttribute("msg", "删除茶林检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause",e.getMessage());
			return ERROR;
		}
		
	}
	
	public String getTeaTreesCheckById(){
		log.debug("茶林检测的id是"+teaTreesCheckId+",正在跳转到更新操作页面...");
		try {
			TeaTreesCheck teaTreesCheck = (TeaTreesCheck) produceService.getById(TeaTreesCheck.class, teaTreesCheckId);
	
			request.setAttribute("teaTreesCheck", teaTreesCheck);
			
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause","更新茶林检测 "+teaTreesCheckId+"，跳转到更新操作页面");
			return ERROR;
		}
	}
	/**
	 * 更新茶林检测信息
	 * @return
	 */
	public String updateTeaTreesCheck(){
		log.debug("更新茶林检测信息");
		try {
			Set<Employee> empsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> devicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			teaTreesCheck.setEmps(empsSet);
			teaTreesCheck.setDevices(devicesSet);
			teaTreesCheck.getDetail().setDescription(teaTreesCheck.getDescription());
			produceService.update(teaTreesCheck);
			request.setAttribute("msg", "更新茶林检测信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
		}
		return ERROR;
	}
	
	
	
	//**************************对培育进行操作         开始**************************//
	/**
	 * 列出某个茶林的培育信息
	 * @param teatreesId
	 */
	public String listBreedInfo(){
		log.debug("列出茶林"+teaTreesId+"的培育信息");
		try {
			List<TeaTreesBreed> teaTreesBreedList = produceService.listByCriteria(TeaTreesBreed.class, 0,"teaTrees.id",teaTreesId);
			request.setAttribute("teaTreesBreedList", teaTreesBreedList);
			request.setAttribute("msg", "列出茶林"+teaTreesId+"的培育信息");
			return SUCCESS;
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	/**
	 * 删除某个茶林的培育信息
	 * @param teatreesId
	 */
	public String deleteBreedInfo(){
		log.debug("删除茶林的一个培育信息，id是"+teaTreesBreedId);
		try {
			produceService.deleteById(TeaTreesBreed.class, teaTreesBreedId);
			request.setAttribute("msg", "删除茶林的一个培育信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	/**
	 * 增加一个茶林的培育信息
	 * @param teatreesId
	 */
	public String addTeaTreesBreed(){
		log.debug("对茶林"+teaTreesId+"增加一个的培育信息");
		try {
			Set<Employee> breedEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> breedDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			teaTreesBreed.setEmps(breedEmpsSet);
			teaTreesBreed.setDevices(breedDevicesSet);
			TeaTrees teaTrees = (TeaTrees) produceService.getById(TeaTrees.class, teaTreesId);
			teaTreesBreed.setTeaTrees(teaTrees);
			teaTreesBreed.setDeleted(false);
			produceService.add(teaTreesBreed);
			request.setAttribute("msg", "对茶林"+teaTreesId+"增加一个的培育信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	
	/**
	 * 得到一个培育信息
	 */
	public String getBreedById(){
		log.debug("茶林培育信息的id是"+teaTreesBreedId+",正在跳转到更新操作页面...");
		try {
			TeaTreesBreed teaTreesBreed = (TeaTreesBreed) produceService.getById(TeaTreesBreed.class, teaTreesBreedId);
			request.setAttribute("teaTreesBreed", teaTreesBreed);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause","更新培育信息"+teaTreesBreedId);
			return ERROR;
		}
	}
	/**
	 * 更新一个培育信息
	 */
	public String updateBreed(){
		log.debug("更新茶林培育信息");
		try {
			Set<Employee> airCheckEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> airCheckDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			teaTreesBreed.setEmps(airCheckEmpsSet);
			teaTreesBreed.setDevices(airCheckDevicesSet);
			produceService.update(teaTreesBreed);
			request.setAttribute("msg", "更新茶林培育信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
		}
		return ERROR;
	}
	
	
	//**************************对防治进行操作         开始**************************//
	
	/**
	 * 增加一个防治信息
	 */
	public String addTeaTreesPrevention(){
		log.debug("对茶林"+teaTreesId+"添加一个防治信息");
		try{
			Set<Employee> breedEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> breedDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			
			teaTreesPrevention.setEmps(breedEmpsSet);
			teaTreesPrevention.setDevices(breedDevicesSet);
			TeaTrees teaTrees = (TeaTrees) produceService.getById(TeaTrees.class, teaTreesId);
			teaTreesPrevention.setTeaTrees(teaTrees);
			produceService.add(teaTreesPrevention);
			request.setAttribute("msg", "对茶林"+teaTreesId+"增加一个的防治信息");
			return SUCCESS;
		}catch(Exception e){
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}	
	}
	/**
	 * 删除一个防治信息
	 * @return
	 */
	public String deletePreventionInfo(){
		log.debug("对茶林删除一个防治信息,防治信息的编号是"+teaTreesPreventionId);
		
		try{
			produceService.deleteById(TeaTreesPrevention.class, teaTreesPreventionId);
			request.setAttribute("msg", "删除编号"+teaTreesPreventionId+"的防治信息");
			return SUCCESS;
		}catch(Exception e){
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	/**
	 * 列出某个茶林的防治信息
	 * @return
	 */
	public String listPreventionInfo(){
		
		log.debug("列出茶林"+teaTreesId+"的防治信息");
		try {
			List<TeaTreesPrevention> teaTreesPreventionList = produceService.listByCriteria(TeaTreesPrevention.class, 0,"teaTrees.id",teaTreesId);
			request.setAttribute("teaTreesPreventionList", teaTreesPreventionList);
			request.setAttribute("msg", "列出茶林"+teaTreesId+"的防治信息");
			return SUCCESS;
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
			return ERROR;
		}
	}
	/**
	 * 得到一个防治信息
	 */
	public String getPreventionById(){
		log.debug("茶林防治信息的id是"+teaTreesPreventionId+",正在跳转到更新操作页面...");
		try {
			TeaTreesPrevention teaTreesPrevention = (TeaTreesPrevention) produceService.getById(TeaTreesPrevention.class, teaTreesPreventionId);
			request.setAttribute("teaTreesPrevention", teaTreesPrevention);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("cause","更新防治信息"+teaTreesPreventionId);
			return ERROR;
		}
	}
	/**
	 * 更新一个防治信息
	 * @return
	 */
	public String updatePrevention(){
		log.debug("更新茶林防治信息");
		try {
			Set<Employee> airCheckEmpsSet = ActionUtils.getInstance().getEmployees(emps.trim());
			Set<Device> airCheckDevicesSet = ActionUtils.getInstance().getDevices(devices.trim());
			teaTreesPrevention.setEmps(airCheckEmpsSet);
			teaTreesPrevention.setDevices(airCheckDevicesSet);
			produceService.update(teaTreesPrevention);
			request.setAttribute("msg", "更新茶林防治信息");
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.debug(e.getMessage());
			request.setAttribute("cause", e.getMessage());
		}
		return ERROR;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//-----------------setter and getter---------------------
	public AirCheckDetail getAirCheckDetail() {
		return airCheckDetail;
	}

	public void setAirCheckDetail(AirCheckDetail airCheckDetail) {
		this.airCheckDetail = airCheckDetail;
	}

	public void setAirCheck(AirCheck airCheck) {
		this.airCheck = airCheck;
	}

	public AirCheck getAirCheck() {
		return airCheck;
	}
	public int getAirCheckId() {
		return airCheckId;
	}

	public void setAirCheckId(int airCheckId) {
		this.airCheckId = airCheckId;
	}

	public int getTeaTreesBreedId() {
		return teaTreesBreedId;
	}

	public void setTeaTreesBreedId(int teaTreesBreedId) {
		this.teaTreesBreedId = teaTreesBreedId;
	}

	public void setEmps(String emps) {
		this.emps = emps;
	}

	public String getEmps() {
		return emps;
	}

	public void setDevices(String devices) {
		this.devices = devices;
	}

	public String getDevices() {
		return devices;
	}

	public void setTeaTreesBreed(TeaTreesBreed teaTreesBreed) {
		this.teaTreesBreed = teaTreesBreed;
	}

	public TeaTreesBreed getTeaTreesBreed() {
		return teaTreesBreed;
	}

	public void setTeaTreesId(int teaTreesId) {
		this.teaTreesId = teaTreesId;
	}

	public int getTeaTreesPreventionId() {
		return teaTreesPreventionId;
	}

	public void setTeaTreesPreventionId(int teaTreesPreventionId) {
		this.teaTreesPreventionId = teaTreesPreventionId;
	}

	public int getTeaTreesId() {
		return teaTreesId;
	}

	public TeaTreesPrevention getTeaTreesPrevention() {
		return teaTreesPrevention;
	}

	public void setTeaTreesPrevention(TeaTreesPrevention teaTreesPrevention) {
		this.teaTreesPrevention = teaTreesPrevention;
	}

	public void setSoilCheckId(int soilCheckId) {
		this.soilCheckId = soilCheckId;
	}

	public int getSoilCheckId() {
		return soilCheckId;
	}

	public void setSoilCheckDetail(SoilCheckDetail soilCheckDetail) {
		this.soilCheckDetail = soilCheckDetail;
	}

	public SoilCheckDetail getSoilCheckDetail() {
		return soilCheckDetail;
	}

	public void setSoilCheck(SoilCheck soilCheck) {
		this.soilCheck = soilCheck;
	}

	public SoilCheck getSoilCheck() {
		return soilCheck;
	}

	public TeaTreesCheckDetail getTeaTreesCheckDetail() {
		return teaTreesCheckDetail;
	}

	public void setTeaTreesCheckDetail(TeaTreesCheckDetail teaTreesCheckDetail) {
		this.teaTreesCheckDetail = teaTreesCheckDetail;
	}

	public TeaTreesCheck getTeaTreesCheck() {
		return teaTreesCheck;
	}

	public void setTeaTreesCheck(TeaTreesCheck teaTreesCheck) {
		this.teaTreesCheck = teaTreesCheck;
	}

	public int getTeaTreesCheckId() {
		return teaTreesCheckId;
	}

	public void setTeaTreesCheckId(int teaTreesCheckId) {
		this.teaTreesCheckId = teaTreesCheckId;
	}

}
